/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.spreedairy.portal.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import com.spreedairy.portal.service.CustomFieldLocalServiceUtil;

import java.io.Serializable;

import java.lang.reflect.Proxy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author SpreeDairy
 */
public class CustomFieldClp extends BaseModelImpl<CustomField>
	implements CustomField {
	public CustomFieldClp() {
	}

	public Class<?> getModelClass() {
		return CustomField.class;
	}

	public String getModelClassName() {
		return CustomField.class.getName();
	}

	public long getPrimaryKey() {
		return _recId;
	}

	public void setPrimaryKey(long primaryKey) {
		setRecId(primaryKey);
	}

	public Serializable getPrimaryKeyObj() {
		return new Long(_recId);
	}

	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("recId", getRecId());
		attributes.put("categoryId", getCategoryId());
		attributes.put("keyValue", getKeyValue());
		attributes.put("keyType", getKeyType());
		attributes.put("optional", getOptional());
		attributes.put("optionsValue", getOptionsValue());
		attributes.put("fieldOrder", getFieldOrder());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long recId = (Long)attributes.get("recId");

		if (recId != null) {
			setRecId(recId);
		}

		Long categoryId = (Long)attributes.get("categoryId");

		if (categoryId != null) {
			setCategoryId(categoryId);
		}

		String keyValue = (String)attributes.get("keyValue");

		if (keyValue != null) {
			setKeyValue(keyValue);
		}

		String keyType = (String)attributes.get("keyType");

		if (keyType != null) {
			setKeyType(keyType);
		}

		Boolean optional = (Boolean)attributes.get("optional");

		if (optional != null) {
			setOptional(optional);
		}

		String optionsValue = (String)attributes.get("optionsValue");

		if (optionsValue != null) {
			setOptionsValue(optionsValue);
		}

		Integer fieldOrder = (Integer)attributes.get("fieldOrder");

		if (fieldOrder != null) {
			setFieldOrder(fieldOrder);
		}
	}

	public long getRecId() {
		return _recId;
	}

	public void setRecId(long recId) {
		_recId = recId;
	}

	public long getCategoryId() {
		return _categoryId;
	}

	public void setCategoryId(long categoryId) {
		_categoryId = categoryId;
	}

	public String getKeyValue() {
		return _keyValue;
	}

	public void setKeyValue(String keyValue) {
		_keyValue = keyValue;
	}

	public String getKeyType() {
		return _keyType;
	}

	public void setKeyType(String keyType) {
		_keyType = keyType;
	}

	public boolean getOptional() {
		return _optional;
	}

	public boolean isOptional() {
		return _optional;
	}

	public void setOptional(boolean optional) {
		_optional = optional;
	}

	public String getOptionsValue() {
		return _optionsValue;
	}

	public void setOptionsValue(String optionsValue) {
		_optionsValue = optionsValue;
	}

	public int getFieldOrder() {
		return _fieldOrder;
	}

	public void setFieldOrder(int fieldOrder) {
		_fieldOrder = fieldOrder;
	}

	public BaseModel<?> getCustomFieldRemoteModel() {
		return _customFieldRemoteModel;
	}

	public void setCustomFieldRemoteModel(BaseModel<?> customFieldRemoteModel) {
		_customFieldRemoteModel = customFieldRemoteModel;
	}

	public void persist() throws SystemException {
		if (this.isNew()) {
			CustomFieldLocalServiceUtil.addCustomField(this);
		}
		else {
			CustomFieldLocalServiceUtil.updateCustomField(this);
		}
	}

	@Override
	public CustomField toEscapedModel() {
		return (CustomField)Proxy.newProxyInstance(CustomField.class.getClassLoader(),
			new Class[] { CustomField.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		CustomFieldClp clone = new CustomFieldClp();

		clone.setRecId(getRecId());
		clone.setCategoryId(getCategoryId());
		clone.setKeyValue(getKeyValue());
		clone.setKeyType(getKeyType());
		clone.setOptional(getOptional());
		clone.setOptionsValue(getOptionsValue());
		clone.setFieldOrder(getFieldOrder());

		return clone;
	}

	public int compareTo(CustomField customField) {
		long primaryKey = customField.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}

		CustomFieldClp customField = null;

		try {
			customField = (CustomFieldClp)obj;
		}
		catch (ClassCastException cce) {
			return false;
		}

		long primaryKey = customField.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(15);

		sb.append("{recId=");
		sb.append(getRecId());
		sb.append(", categoryId=");
		sb.append(getCategoryId());
		sb.append(", keyValue=");
		sb.append(getKeyValue());
		sb.append(", keyType=");
		sb.append(getKeyType());
		sb.append(", optional=");
		sb.append(getOptional());
		sb.append(", optionsValue=");
		sb.append(getOptionsValue());
		sb.append(", fieldOrder=");
		sb.append(getFieldOrder());
		sb.append("}");

		return sb.toString();
	}

	public String toXmlString() {
		StringBundler sb = new StringBundler(25);

		sb.append("<model><model-name>");
		sb.append("com.spreedairy.portal.model.CustomField");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>recId</column-name><column-value><![CDATA[");
		sb.append(getRecId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>categoryId</column-name><column-value><![CDATA[");
		sb.append(getCategoryId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>keyValue</column-name><column-value><![CDATA[");
		sb.append(getKeyValue());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>keyType</column-name><column-value><![CDATA[");
		sb.append(getKeyType());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>optional</column-name><column-value><![CDATA[");
		sb.append(getOptional());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>optionsValue</column-name><column-value><![CDATA[");
		sb.append(getOptionsValue());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>fieldOrder</column-name><column-value><![CDATA[");
		sb.append(getFieldOrder());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _recId;
	private long _categoryId;
	private String _keyValue;
	private String _keyType;
	private boolean _optional;
	private String _optionsValue;
	private int _fieldOrder;
	private BaseModel<?> _customFieldRemoteModel;
}